<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="单据编号" prop="vBillNo">
        <el-input
          v-model="queryParams.vBillNo"
          placeholder="请输入单据编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="执行日期" prop="dExecute">
        <el-date-picker
          clearable
          v-model="queryParams.dExecute"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择执行日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="项目编号" prop="vProjectNo">
        <el-input
          v-model="queryParams.vProjectNo"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="applyBy">
        <el-input
          v-model="queryParams.applyBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="经办人" prop="oprBy">
        <el-input
          v-model="queryParams.oprBy"
          placeholder="请输入经办人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否转一般库存" prop="isTransform">
        <el-select
          v-model="queryParams.isTransform"
          placeholder="请选择是否转一般库存"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="backbillList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="单据编号" align="center" prop="vBillNo" />

      <el-table-column
        label="执行日期"
        align="center"
        prop="dExecute"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dExecute, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="项目编号" align="center" prop="vProjectNo" />
      <el-table-column label="申请人" align="center" prop="applyBy" />
      <el-table-column label="经办人" align="center" prop="oprBy" />
      <el-table-column label="状态" align="center" prop="status">
        <!-- <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_normal_disable"
            :value="scope.row.status"
          /> -->
        <!-- </template> -->
      </el-table-column>
      <el-table-column label="是否转一般库存" align="center" prop="isTransform">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_yes_no"
            :value="scope.row.isTransform"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退库单对话框 -->
    <el-drawer
      :title="title"
      :visible.sync="open"
      size="70%"
      ref="drawer"
      append-to-body
    >
      <div class="drawer-container">
        <div class="xin-main">
          <div class="xin-content" style="height: 90%">
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
              <el-row>
                <el-col :span="8">
                  <el-form-item label="单据编号" prop="vBillNo">
                    <el-input
                      v-model="form.vBillNo"
                      placeholder="请输入单据编号"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="执行日期" prop="dExecute">
                    <el-date-picker
                      clearable
                      v-model="form.dExecute"
                      type="date"
                      value-format="yyyy-MM-dd"
                      placeholder="请选择执行日期"
                    >
                    </el-date-picker>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="项目编号" prop="vProjectNo">
                    <el-input
                      v-model="form.vProjectNo"
                      placeholder="请输入项目编号"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="申请人" prop="applyBy">
                    <el-input
                      v-model="form.applyBy"
                      placeholder="请输入申请人"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="经办人" prop="oprBy">
                    <el-input v-model="form.oprBy" placeholder="请输入经办人" />
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item
                    label="是否转一般库存"
                    prop="isTransform"
                    label-width="120px"
                  >
                    <el-radio-group v-model="form.isTransform">
                      <el-radio
                        v-for="dict in dict.type.sys_yes_no"
                        :key="dict.value"
                        :label="dict.value"
                        >{{ dict.label }}</el-radio
                      >
                    </el-radio-group>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="8">
                  <el-form-item label="备注" prop="remark">
                    <el-input v-model="form.remark" placeholder="请输入备注" />
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <el-divider content-position="left"></el-divider>
            <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                <el-button
                  type="primary"
                  plain
                  icon="el-icon-plus"
                  size="mini"
                  @click="handleTableAdd"
                  >新增</el-button
                >
              </el-col>
            </el-row>
            <el-table :data="executeList" min-height="450px" border>
              <el-table-column
                label="物料名称"
                align="center"
                prop="vSpaName"
              />
              <el-table-column
                label="规格型号"
                align="center"
                prop="vSpaModel"
              />
              <el-table-column label="品牌" align="center" prop="vSpaBrand" />
              <el-table-column label="数量" align="center" prop="nQty">
                <template slot-scope="scope">
                  <el-input
                    size="small"
                    v-model="scope.row.nQty"
                    @change="handleEdit(scope.$index, scope.row)"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column label="仓库" align="center" prop="vWHName" />
              <el-table-column label="库区" align="center" prop="vAreaName" />
              <el-table-column
                label="库位"
                align="center"
                prop="vLocationName"
              />

              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="itemDelete(scope.$index, scope.row)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
            <div
              class="xin-footer"
              style="position: fixed; bottom: 5px; right: 10px"
            >
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button type="primary" @click="executeBack"
                >执行退库</el-button
              >
              <el-button @click="cancel">取 消</el-button>
            </div>
          </div>
        </div>
      </div>
    </el-drawer>
    <el-dialog
      title="库存信息"
      :visible.sync="addOpen"
      v-if="addOpen"
      width="50%"
      append-to-body
    >
      <add :sendData="sendData" @close="getAddReturn" />
    </el-dialog>
  </div>
</template>

<script>
import {
  listBackbill,
  getBackbill,
  delBackbill,
  addBackbill,
  updateBackbill,
  delItem,
  checkInvQty,
  executeBack,
} from "@/api/inv/inv0008";
import add from "./add";

export default {
  name: "Backbill",
  dicts: ["inv_iotpye", "inv_bill_type", "sys_yes_no", "sys_normal_disable"],
  components: { add },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 退库单表格数据
      backbillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        vBillNo: null,
        iotype: null,
        vType: null,
        dExecute: null,
        vProjectNo: null,
        applyBy: null,
        oprBy: null,
        status: null,
        isTransform: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      executeList: [],
      addOpen: false,
      sendData: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询退库单列表 */
    getList() {
      this.loading = true;
      listBackbill(this.queryParams).then((response) => {
        this.backbillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        vBillNo: null,
        iotype: null,
        vType: null,
        dExecute: this.getCurrentDate(),
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        vProjectNo: null,
        applyBy: null,
        oprBy: null,
        status: null,
        isTransform: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.executeList = [];
      this.open = true;
      this.title = "添加退库单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();

      getBackbill(row).then((response) => {
        this.form = response.data.invBackboundBill;
        this.executeList = response.data.invOutboundBillItemList;
        this.open = true;
        this.title = "修改退库单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let para = {};
          para = {
            invBackboundBill: this.form,
            invOutboundBillItemList: this.executeList,
          };
          if (this.form.id != null) {
            updateBackbill(para).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBackbill(para).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除退库单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBackbill(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/backbill/export",
        {
          ...this.queryParams,
        },
        `backbill_${new Date().getTime()}.xlsx`
      );
    },
    itemDelete(index, row) {
      debugger;
      var _this = this;
      this.$modal
        .confirm("是否确认删除该条数据项？")
        .then(function () {
          debugger;
          if (
            row.id === "" ||
            row.id === null ||
            row.id === undefined ||
            row.id === 0
          ) {
            _this.executeList.splice(index, 1);
            console.log(_this.executeList);
          } else {
            delItem(row).then((response) => {
              _this.executeList.splice(index, 1);
              console.log(_this.executeList);
            });
            console.log("$$$$$$$$$$$$$$$");
            console.log(_this.executeList);
          }
        })
        .then(() => {
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleTableAdd() {
      var _this = this;
      _this.addOpen = true;
      _this.sendData = { executeList: _this.executeList, formData: _this.form };
    },
    getAddReturn(type, data) {
      this.executeList = data;
      this.addOpen = false;
    },
    executeBack() {
      let para = {};
      para = {
        invBackboundBill: this.form,
        invOutboundBillItemList: this.executeList,
      };
      executeBack(para).then((response) => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },

    handleEdit(index, row) {
      row.vProjectNo = this.form.vProjectNo;
      checkInvQty(row).then((response) => {});
    },
    getCurrentDate() {
      const date = new Date();
      // 格式化日期为YYYY-MM-DD形式
      let year = date.getFullYear().toString();
      let month = (date.getMonth() + 1).toString().padStart(2, "0");
      let day = date.getDate().toString().padStart(2, "0");
      // 更新currentDate变量值
      let currentDate = `${year}-${month}-${day}`;
      return currentDate;
    },
  },
};
</script>
